package com.hqyj.freemovie.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.freemovie.front.domain.UsersPermission;
import com.hqyj.freemovie.front.mapper.UsersPermissionMapper;
import com.hqyj.freemovie.front.service.UsersPermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UsersPermissionServiceImpl extends ServiceImpl<UsersPermissionMapper, UsersPermission> implements UsersPermissionService {

    @Resource
    private UsersPermissionMapper usersPermissionMapper;

    @Override
    public boolean savePermissions(UsersPermission usersPermission) throws Exception {
        try {
            usersPermissionMapper.insert(usersPermission);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Override
    public List<String> queryUserPermissionsByUid(Integer uid) {
        QueryWrapper<UsersPermission> queryWrapper = new QueryWrapper<UsersPermission>();
        queryWrapper.eq("uid", uid);
        List<UsersPermission> usersPermissionList = usersPermissionMapper.selectList(queryWrapper);
        List<String> list = new ArrayList<String>();
        if (usersPermissionList != null && usersPermissionList.size() > 0) {
            for (UsersPermission usersPermission : usersPermissionList) {
                list.add(usersPermission.getPermission());
            }
        }
        return list;
    }
}
